package net.siren93.smh.action;

import net.siren93.smh.entity.EmployeeEntity;
import net.siren93.smh.entity.request.EmployeeRequest;
import net.siren93.smh.entity.response.BaseResponse;
import net.siren93.smh.entity.response.EmployeeNameResponse;
import net.siren93.smh.entity.response.EmployeePageResponse;
import net.siren93.smh.service.EmployeeService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by siren93 on 15/9/25.
 */
@Controller
@RequestMapping("/employee")
public class EmployeeAction {

    @Autowired
    private EmployeeService employeeService;

    private final Logger LOGGER = Logger.getLogger(EmployeeAction.class);

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public @ResponseBody
    BaseResponse login(@RequestBody EmployeeEntity entity) {
        EmployeeEntity employee = employeeService.findByName(entity.getName());
        if(employee == null || !employee.getPassword().equals(entity.getPassword())) {
            return new BaseResponse(BaseResponse.STATUS_FAILED, "登录失败");
        } else {
            BaseResponse response = new BaseResponse(BaseResponse.STATUS_SUCCESS, "登录成功");
            response.setData(employee.getId());
            return response;
        }
    }

    @RequestMapping(value = "/getNames", method = RequestMethod.GET)
    public @ResponseBody
    List<EmployeeNameResponse> getNames(){
        List<EmployeeEntity> entities = employeeService.findAll();
        List<EmployeeNameResponse> responses = new ArrayList<>();
        for(EmployeeEntity entity : entities){
            EmployeeNameResponse response = new EmployeeNameResponse();
            response.setEmployeeId(entity.getId());
            response.setEmployeeName(entity.getName());
            responses.add(response);
        }
        return responses;
    }

    @RequestMapping(value = "/allEmployee", method = RequestMethod.POST)
    public  @ResponseBody
    EmployeePageResponse getEmployees(@CookieValue(value = "id", required = true) int adminId,
                                      @RequestParam(value = "page", defaultValue = "1") int page,
                                      @RequestParam(value = "rows", defaultValue = "10") int rows){
        EmployeeEntity employee = employeeService.findById(adminId);
        List<EmployeeEntity> employees = null;
        if(employee.getWorkYears() > 3) {
            employees = employeeService.findByPage(page, rows);
        } else {
            employees = new ArrayList<>();
            employees.add(employee);
        }
        EmployeePageResponse response = new EmployeePageResponse();
        response.setTotal(employeeService.findAll().size());
        response.setRows(employees);
        return response;
    }

    @RequestMapping(value = "/manageEmployee", method = RequestMethod.GET)
    public String manageEmployee(){
        return "/employee";
    }

    @RequestMapping(value = "/addOrUpdateEmployees", method = RequestMethod.POST)
    public @ResponseBody
    BaseResponse updateEmployee(@RequestBody EmployeeRequest[] employees){
        try{
            for(EmployeeRequest employee: employees){
                EmployeeEntity entity = employeeService.findById(employee.getId());
                if(entity == null) {
                    entity = new EmployeeEntity();
                    entity.setWorkYears(0);
                }
                entity.setName(employee.getName());
                entity.setPassword(employee.getPassword());
                entity.setGender(employee.getGender());
                entity.setAge(employee.getAge());
                String dateStr = employee.getBirthday();
                SimpleDateFormat fm = new SimpleDateFormat("MM/dd/yy");
                java.util.Date date = fm.parse(dateStr);
                entity.setBirthday(new Date(date.getTime()));
                employeeService.saveOrUpdate(entity);
            }
            return new BaseResponse(BaseResponse.STATUS_SUCCESS, "成功");
        } catch (Exception e){
            return new BaseResponse(BaseResponse.STATUS_FAILED, e.getMessage());
        }
    }

}
